home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Internet / gpodder / gpodder-3.8.3-setup.exe / {app} / src / mygpoclient / json_test.py < prev    next >
Text File  |  2013-02-08  |  2KB  |  64 lines

  1. # -*- coding: utf-8 -*-
  2. # gpodder.net API Client
  3. # Copyright (C) 2009-2013 Thomas Perl and the gPodder Team
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17.  
  18. from StringIO import StringIO
  19. import urllib2
  20.  
  21. from mygpoclient import http
  22. from mygpoclient import json
  23.  
  24. import unittest
  25. import minimock
  26.  
  27. class Test_JsonClient(unittest.TestCase):
  28.     USERNAME = 'john'
  29.     PASSWORD = 'secret'
  30.  
  31.     def setUp(self):
  32.         self.mockopener = minimock.Mock('urllib2.OpenerDirector')
  33.         urllib2.build_opener = minimock.Mock('urllib2.build_opener')
  34.         urllib2.build_opener.mock_returns = self.mockopener
  35.  
  36.     def tearDown(self):
  37.         minimock.restore()
  38.  
  39.     def mock_setHttpResponse(self, value):
  40.         self.mockopener.open.mock_returns = StringIO(value)
  41.  
  42.     def test_parseResponse_worksWithDictionary(self):
  43.         client = json.JsonClient(self.USERNAME, self.PASSWORD)
  44.         self.mock_setHttpResponse('{"a": "B", "c": "D"}')
  45.         items = list(sorted(client.GET('/').items()))
  46.         self.assertEquals(items, [('a', 'B'), ('c', 'D')])
  47.  
  48.     def test_parseResponse_worksWithIntegerList(self):
  49.         client = json.JsonClient(self.USERNAME, self.PASSWORD)
  50.         self.mock_setHttpResponse('[1,2,3,6,7]')
  51.         self.assertEquals(client.GET('/'), [1,2,3,6,7])
  52.  
  53.     def test_parseResponse_emptyString_returnsNone(self):
  54.         client = json.JsonClient(self.USERNAME, self.PASSWORD)
  55.         self.mock_setHttpResponse('')
  56.         self.assertEquals(client.GET('/'), None)
  57.  
  58.     def test_invalidContent_raisesJsonException(self):
  59.         client = json.JsonClient(self.USERNAME, self.PASSWORD)
  60.         self.mock_setHttpResponse('this is not a valid json string')
  61.         self.assertRaises(json.JsonException, client.GET, '/')
  62.  
  63.  
  64.